package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Setmeal;
import com.itheima.bean.SetmealDish;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.dto.SetmealDto;
import com.itheima.mapper.SetmealDishMapper;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 要将数据加入setmeal 和 setmeal_dish 两张表
     * @param setmealDto 根据前端传回的数据，需要用数据传输对象接收
     * @return 无所谓
     */
    @PostMapping
    public R save(@RequestBody SetmealDto setmealDto){
        //1.将套餐存入套餐表
        setmealService.save(setmealDto);
        //2.处理后将dish(变异的)存入setmeal_dish表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish :setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
        return R.success("添加成功");
    }


    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page,int pageSize,String name){
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Setmeal::getUpdateTime).like(name!=null,Setmeal::getName,name);
        return setmealService.pageR(setmealPage,wrapper);
    }

    /**
     * 删除setmeal中数据 以及 setmeal_dish中套餐id相关数据
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        setmealService.removeBatchByIds(ids);

        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        for (Long id :ids) {
            setmealDishService.
                    remove(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId,id));
        }
        return R.success("删除成功");
    }

    /**
     * 为修改套餐回显套餐数据
     * @param id
     * @return   因为需要有菜品(Dish)，所以返回封装了 List<SetmealDish> setmealDishes 的SetmealDto
     */
    @GetMapping("/{id}")
    public R<SetmealDto> reShowForUpdate(@PathVariable int id){
        //1.查出id是id的Setmeal
        //2.查出多条setmeal_id是id的SetmealDish
        //3.初始化Dto，并返回

        Setmeal setmeal = setmealService.getById(id);

        List<SetmealDish> setmealDishes =
                setmealDishMapper.
                        selectList(new LambdaQueryWrapper<SetmealDish>().
                                eq(SetmealDish::getSetmealId, id));

        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(setmealDishes);

        return R.success(setmealDto);
    }

    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto){
        //1.setmeal表直接修改
        setmealService.updateById(setmealDto);
        //2.setmeal_dish表先删后插入，插入前先添加setmeal_id
        setmealDishMapper.delete(new LambdaQueryWrapper<SetmealDish>().
                eq(SetmealDish::getSetmealId,setmealDto.getId()));

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        for (SetmealDish setmealDish :setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }


        setmealDishService.saveBatch(setmealDishes);
        return R.success("修改成功");
    }
}
